package com.wframe.project.former.web.marketing;


import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.frame.pageform.DataPageForm;
import com.wframe.project.former.dataservices.marketing.PromotionsService;
import com.wframe.project.former.dataservices.marketing.SingService;
import com.wframe.project.former.dataservices.products.ProductClassService;
import com.wframe.project.former.dataservices.products.ProductService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONArray;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/25.
 */
public class SinglePromotionPage extends DataPageForm {
    private static SingService singService = SingService.getIntance();
    private static PromotionsService promotionsService = PromotionsService.getIntance();
    private JdataBean bean;


    @Override
    public String execute() throws Exception {
        setPagination(singService.querySinLe(bean,getPageSize_(), getPageNum()));
        return "/webadmin/marketing/singleList.jsp";
    }

    @Override
    public String show() throws Exception {
        bean = singService.selectSinLeByid(bean.getInt("id"));
        if (StringUtils.isNotBlank(bean.getString("product_val"))) {  //查询参加活动的商品信息
            JdataBean selectedPro = singService.querySelectedProduct(bean.getInt("product_val"));
            if (selectedPro != null) {
                selectedPro.put("types",promotionsService.getProductMoneyOffTypeById(bean.getString("product_val")));
                ProductClassService pcservice = ProductClassService.getIntance();
                ProductService service = ProductService.getIntance();
                selectedPro.put("price",promotionsService.getProducrPrice(String.valueOf(selectedPro.get("id"))));
                List<JdataBean> jdataBeans = service.queryAllModelAttrAndVal("1", String.valueOf(selectedPro.get("id")), 1);
                for (JdataBean attr : jdataBeans) {
                    selectedPro.put(attr.getString("code"), attr.get("val"));
                }
                selectedPro.put("classname", pcservice.getProductClassName(String.valueOf(selectedPro.getString("id"))));
                getRequest().setAttribute("selectedPro",selectedPro);
            }
        }
        getRequest().setAttribute("controlsales", promotionsService.promotionControlsale(bean.getInt("promotionid")));

        return "/webadmin/marketing/singleListEdit.jsp";
    }

    @Override
    public String toDel() throws Exception {
        return null;
    }

    @Override
    public boolean toUpdate() throws Exception {
        try {
            JdataBean basedata = new JdataBean();
            basedata.put("platform", bean.getString("platform"));
            basedata.put("start_time", bean.getString("start_time"));
            basedata.put("end_time", bean.getString("end_time"));
            basedata.put("description", bean.getString("description"));
            basedata.put("time", bean.getString("time"));
            basedata.put("name", bean.getString("name"));
            basedata.put("sort_value",bean.getString("sort_value"));
            basedata.put("district_description",bean.getString("district_description"));
            basedata.put("show_sell",bean.getString("show_sell"));
            if (bean.getString("product_val") == null) {
                basedata.put("product_val", null);
                basedata.put("product_type", null);
            }


            if(StringUtils.isNotBlank(bean.getString("product_val"))){
                basedata.put("product_type", 1);
                basedata.put("product_val", bean.getString("product_val"));
            }
            Map key = new HashMap();
            key.put("id", bean.getString("promotionid"));

            //查看该商品有无参加过单品促销活动
            String productjoin =promotionsService.productSingle(bean.getString("product_val"));

            if(StringUtils.isNotBlank(productjoin)){
                if (!productjoin.equals(bean.getString("promotionid"))) {
                    getJsonObject().put("state","no");
                    return false;
                }
            }

            //保存基础促销数据
            if (StringUtils.isNotBlank(bean.getString("promotionid"))) {
                promotionsService.update(basedata.getDataMap(), key);
            }
            //单品促销数据
            JdataBean flashSaleData = new JdataBean();
            flashSaleData.put("amount", bean.getString("amount"));
            flashSaleData.put("price", bean.getString("price"));
            flashSaleData.put("count_down", bean.getString("count_down"));
            key.put("id", bean.getInt("flashsaleid"));

            //保存基础促销数据
            if (StringUtils.isNotBlank(bean.getString("flashsaleid"))) {
                if (bean.getString("product_val") == null) {
                    flashSaleData.put("name",null);
                }
                if(StringUtils.isNotBlank(bean.getString("product_val"))){
                    JdataBean jdataBean = singService.queryproduct(bean.getInt("product_val"));
                    flashSaleData.put("name", jdataBean.getString("name"));
                }
                singService.update(flashSaleData.getDataMap(), key);
            }

            //添加单品促销
            if (StringUtils.isBlank(bean.getString("promotionid")) && StringUtils.isBlank(bean.getString("flashsaleid"))) {
                basedata.put("create_time", new Date());
                if (bean.getString("product_val") != null) {
                    basedata.put("product_type", 1);
                }
                basedata.put("promotion_type", 0);
                basedata.put("weight",0);
                basedata.put("second_priority",1);
                String pid = promotionsService.addPromotions(basedata);    //创建单品促销promotions
                if (bean.getString("product_val") != null) {
                    JdataBean jdataBean  = singService.queryproduct(bean.getInt("product_val"));
                    flashSaleData.put("name", jdataBean.getString("name"));
                }
                flashSaleData.put("promotionid", pid);
                singService.save(flashSaleData.getDataMap());

                JSONArray controlsales = null;
                if (StringUtils.isNotBlank(bean.getString("controlsales")))//保存控销数据
                    controlsales = new JSONArray(bean.getString("controlsales"));
                singService.addControlsalesSingLe(pid, controlsales, bean.getString("controlusers"));
            }

            if (StringUtils.isNotBlank(bean.getString("promotionid"))) {
                JSONArray controlsales = null;
                if (StringUtils.isNotBlank(bean.getString("controlsales")))//保存控销数据
                    controlsales = new JSONArray(bean.getString("controlsales"));
                singService.addControlsalesSingLe(bean.getString("promotionid"), controlsales, bean.getString("controlusers"));
            }

            getJsonObject().put("state", "ok");
        } catch (Exception e) {
            Logger.getLogger(this.getClass()).error("保存商品信息错误：", e);
            getJsonObject().put("state", e.getMessage());
        }
        return false;
    }

    public String singLeAdd() throws Exception {
        return "/webadmin/marketing/singLeAdd.jsp";
    }

    //选择商品
    public String chooseProductList() throws Exception {
        PageResult  pr = singService.queryChooseProduct(bean, 10, getPageNum());
        ProductClassService pcservice = ProductClassService.getIntance();
        ProductService service = ProductService.getIntance();
        List<JdataBean> list = pr.getList();

        for (JdataBean product : list) {
            product.put("price",promotionsService.getProducrPrice(String.valueOf(product.get("id"))));
            product.put("moneyOffType", promotionsService.getProductMoneyOffTypeById(String.valueOf(product.get("id"))));
            product.put("products_types",promotionsService.getProductType(String.valueOf(product.get("id"))));
            List<JdataBean> jdataBeans = service.queryAllModelAttrAndVal("1", String.valueOf(product.get("id")), 1);
            for (JdataBean attr : jdataBeans) {
                product.put(attr.getString("code"), attr.get("val"));
            }
//            product.put("promotion_type", singService.queryType(product.getInt("id")));
//            product.put("promotiontype",promotionsService.queryType());
            product.put("classname", pcservice.getProductClassName(String.valueOf(product.getString("id"))));

        }

        setPagination(pr);
        return "/webadmin/marketing/chooseProductList.jsp";
    }
    public String updataState() throws Exception {
        promotionsService.updataStop(bean.getInt("promotionid"),bean.getInt("state"));
        return getJsonObject().toString();
    }
    public String checkProduct() throws Exception{
        PageResult pr = singService.checkProduct(bean,getPageSize_(),getPageNum());
        ProductClassService pcservice = ProductClassService.getIntance();
        ProductService service = ProductService.getIntance();
        List<JdataBean> list = pr.getList();
        for (JdataBean product : list) {
            List<JdataBean> jdataBeans = service.queryAllModelAttrAndVal("1", String.valueOf(product.get("id")), 1);
            for (JdataBean attr : jdataBeans) {
                product.put(attr.getString("code"), attr.get("val"));
            }
            product.put("classname", pcservice.getProductClassName(String.valueOf(product.getString("id"))));

        }
        setPagination(pr);
        return "/webadmin/marketing/chooseProductList.jsp";
    }

    //判断是否有重复的商品提交
    public String productSingLe() throws Exception{
        String name = promotionsService.productSingle(bean.getString("primarykey"));
        getRequest().setAttribute("productSingle",name);
        return "/webadmin/marketing/chooseProductList.jsp";
    }


    public JdataBean getBean() {
        return bean;
    }

    public void setBean(JdataBean bean) {
        this.bean = bean;
    }
}
